﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: Propietario			*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.MVP;
using ESF.General;
using ESF.SSO.Core.Entities;
using ESF.General.Entity;

#endregion 


namespace ESF.SSO.Web.Tools.Presenter
{
	public class SignInPresenter : BasePresenter<ISignInViewer>
	{
		#region Constructor
		
		public SignInPresenter (ISignInViewer pViewer) : base (pViewer) {}
		
		#endregion
		
		
		#region Funciones

		public override void Load()
		{
			
		}
		
		
		public override void Clear ()
		{
			_Viewer.SignedInUser = null;
			
			_Viewer.UserAlias = string.Empty;
			_Viewer.UserPassword = string.Empty;
		}
		
		
		public bool SignInByCredentials (int pSessionTimeoutMinutes)
		{
			User user;
			
		
			user = new User () 
			{
				Alias = _Viewer.UserAlias,
				Password = _Viewer.UserPassword, 
				SessionRemember = _Viewer.SessionRemember,
				UserTerminal = new Terminal () { UserHostAddress = _Viewer.frmUserHostAddress }
			};
		
			try
			{
				using (SSOServiceClient.SSOServiceClient ssoClient = new SSOServiceClient.SSOServiceClient ())
				{
					user = ssoClient.SignInByCredentials (user);
				
					user.SessionRemember = _Viewer.SessionRemember;
					user.SessionTimeoutMinutes = pSessionTimeoutMinutes;
					user.UserTerminal = new Terminal () { UserHostAddress = _Viewer.frmUserHostAddress };
					
					_Viewer.SignedInUser = user;
				}
				
				return true;
			}
			catch (Exception pEx)
			{
				LogError (pEx);
				
				return false;
			}
		}
		
		#endregion
	}
}
